//
//  InputPayItemView.m
//  BaseProject
//
//  Created by 王盼盼 on 2024/9/6.
//  Copyright © 2024 wangpanpan. All rights reserved.
//

#import "InputPayItemView.h"
#import "InputPayItemModel.h"
@interface InputPayItemView ()<UITextFieldDelegate>
@property(nonatomic, strong) UIImageView *iconImageView;
@property(nonatomic, strong) UILabel *titleLabel;
@property(nonatomic, strong) UIImageView *flagImageView;
@property(nonatomic, strong) UILabel *subTitleLabel;
@property(nonatomic, strong) UITextField *textField;
@property(nonatomic, strong) UILabel *periodsTipLabel;
@end

@implementation InputPayItemView
- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        [self makeUI];
    }
    return self;
}
- (void)makeUI {
    [self addSubview:self.iconImageView];
    [self addSubview:self.titleLabel];
    [self addSubview:self.lineView];
    [self addSubview:self.flagImageView];
    [self addSubview:self.subTitleLabel];
    
    self.iconImageView.frame = CGRectMake(15, 15, 38, 38);
    self.titleLabel.frame = CGRectMake(self.iconImageView.right + 20, 0, 200, 20);
    self.flagImageView.frame = CGRectMake(self.width - 40, 0, 16, 16);
    self.subTitleLabel.frame = CGRectMake(15, self.iconImageView.bottom + 15, 200, 20);
    self.flagImageView.centerY = self.iconImageView.centerY;
    self.titleLabel.centerY = self.iconImageView.centerY;
    self.height = self.subTitleLabel.bottom + 15;
}
- (void)layoutSubviews {
    [super layoutSubviews];
    self.lineView.frame = CGRectMake(0, self.height - 0.5, self.width, 0.5);
}
- (void)setData:(InputPayItemModel *)data {
    _data = data;
    self.iconImageView.image = [UIImage imageNamed:data.localImagePath];
    self.titleLabel.text = data.title;
    self.subTitleLabel.text = data.subTitle;
    self.flagImageView.image = [UIImage imageNamed:data.isSelect ? @"common_sel" : @"common_nor"];
    if (data.hasPeriods) {
        [self addSubview:self.textField];
        [self addSubview:self.periodsTipLabel];
        self.textField.frame = CGRectMake(15, self.iconImageView.bottom + 15, 100, 30);
        if (data.isSelect) {
            self.periodsTipLabel.hidden = NO;
            self.textField.hidden = NO;
            self.subTitleLabel.top = self.textField.bottom + 15;
            self.periodsTipLabel.frame = CGRectMake(self.textField.right + 10, self.textField.top, 30, self.textField.height);
        } else {
            self.periodsTipLabel.hidden = YES;
            self.textField.hidden = YES;
            self.subTitleLabel.top = self.iconImageView.bottom + 15;
        }
        self.height = self.subTitleLabel.bottom + 15;

        self.textField.text = [NSString stringWithFormat:@"%ld", data.periods];
        self.periodsTipLabel.text = @"期";
    }
}
#pragma mark - UITextField
- (void)editingChangedTextField:(UITextField *)textField {
    if (IsStrEmpty(textField.text)) {
        self.data.periods = 1;
        return;
    }
    self.data.periods = textField.text.integerValue;
    self.textField.text = [NSString stringWithFormat:@"%ld", self.data.periods];
}
- (void)textFieldDidEndEditing:(UITextField *)textField {
    if (IsStrEmpty(textField.text)) {
        self.data.periods = 1;
        self.textField.text = [NSString stringWithFormat:@"%ld", self.data.periods];
    }
    if (self.data.periods <= 0) {
        self.data.periods = 1;
        self.textField.text = [NSString stringWithFormat:@"%ld", self.data.periods];
    }
}
#pragma mark - lazy
- (UIImageView *)iconImageView {
    if (!_iconImageView) {
        _iconImageView = [[UIImageView alloc]init];
        _iconImageView.contentMode = UIViewContentModeScaleAspectFit;
    }
    return _iconImageView;
}
- (UILabel *)titleLabel {
    if (!_titleLabel) {
        UILabel *lab = [[UILabel alloc]init];
        lab.font = [UIFont systemFontOfSize:15 weight:UIFontWeightMedium];
        lab.textColor = rgba(51, 51, 51, 1);
        lab.textAlignment = NSTextAlignmentLeft;
        lab.numberOfLines = 1;
        _titleLabel = lab;
    }
    return _titleLabel;
}
- (UILabel *)subTitleLabel {
    if (!_subTitleLabel) {
        UILabel *lab = [[UILabel alloc]init];
        lab.font = [UIFont systemFontOfSize:15 weight:UIFontWeightMedium];
        lab.textColor = kRedColor;
        lab.textAlignment = NSTextAlignmentLeft;
        lab.numberOfLines = 1;
        _subTitleLabel = lab;
    }
    return _subTitleLabel;
}
- (UILabel *)periodsTipLabel {
    if (!_periodsTipLabel) {
        UILabel *lab = [[UILabel alloc]init];
        lab.font = [UIFont systemFontOfSize:13 weight:UIFontWeightRegular];
        lab.textColor = kColor184;
        lab.textAlignment = NSTextAlignmentLeft;
        lab.numberOfLines = 1;
        _periodsTipLabel = lab;
    }
    return _periodsTipLabel;
}
- (UIView *)lineView {
    if (!_lineView) {
        UIView *view = [[UIView alloc]init];
        view.backgroundColor = kLineColor;
        _lineView = view;
    }
    return _lineView;
}
- (UIImageView *)flagImageView {
    if (!_flagImageView) {
        _flagImageView = [[UIImageView alloc]init];
    }
    return _flagImageView;
}
- (UITextField *)textField {
    if (!_textField) {
        UITextField *tf = [[UITextField alloc]initWithFrame:CGRectMake(20, 30, kScreenWidth - 40, 50)];
        tf.textColor = kColor51;
        NSMutableAttributedString *placeHolder = [[NSMutableAttributedString alloc]initWithString:@"请输入分期数"];
        placeHolder.color = kColor184;
        placeHolder.font = [UIFont systemFontOfSize:12 weight:UIFontWeightBold];
        tf.attributedPlaceholder = placeHolder;
        tf.font = [UIFont systemFontOfSize:16 weight:UIFontWeightBold];
        tf.clearButtonMode = UITextFieldViewModeNever;
        tf.keyboardType = UIKeyboardTypeNumberPad;//UIKeyboardTypeNumberPad UIKeyboardTypeDecimalPad 小数点
        tf.secureTextEntry = NO;
        tf.tintColor = kGreenColor;
        tf.delegate = self;
        [tf addTarget:self action:@selector(editingChangedTextField:) forControlEvents:UIControlEventEditingChanged];
        tf.textAlignment = NSTextAlignmentCenter;
        tf.borderStyle = UITextBorderStyleNone;
        tf.layer.cornerRadius = 4;
        tf.layer.borderColor = rgba(244, 244, 244, 1).CGColor;
        tf.layer.borderWidth = 1;
        _textField = tf;
    }
    return _textField;
}
@end
